//
//  ExamSignUpTableViewCellFirst.h
//  CommunityNannyO2O
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "ExamSignUpTableViewCellFirst.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "CustomTextField.h"
#import "AppConfig.h"
#import "ExamSignUpTableViewCellFirst.h"
#import "QXNetWorkTool.h"

@interface ExamSignUpTableViewCellFirst()<QXNetWorkToolDelegate>


@property (weak, nonatomic) IBOutlet UIImageView *head;
@property (weak, nonatomic) IBOutlet UIButton *headPhotoButton;
@property (weak, nonatomic) IBOutlet UIImageView *Photo;

@property (weak, nonatomic) IBOutlet CustomTextField  *nickName;

@property (weak, nonatomic) IBOutlet CustomTextField  *mail;

@property (weak, nonatomic) IBOutlet CustomTextField  *RealName;

@property (weak, nonatomic) IBOutlet CustomTextField  *idNumber;

@property (weak, nonatomic) IBOutlet UIButton *finish;

@property UIImagePickerController *mediaPicker;
@property UIImagePickerController *headPicker;

@end

@implementation ExamSignUpTableViewCellFirst

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"ExamSignUpTableViewCellFirst";
    ExamSignUpTableViewCellFirst *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil]firstObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}

-(void)awakeFromNib
{
    [self.nickName setFont:[UIFont systemFontOfSize:12] Color:UIColorRGBA(0, 0, 0, 1)];
    
    //头像
    [self.head.layer setCornerRadius:self.head.frame.size.width / 2];
    self.head.clipsToBounds = YES;
    self.head.contentMode = UIViewContentModeScaleAspectFill;
    
    
    //叉号，一次性删除输入框中的内容
    [self.nickName setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.mail setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.RealName setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.idNumber setClearButtonMode:UITextFieldViewModeWhileEditing];
    
    //数字键盘
    self.idNumber.keyboardType = UIKeyboardTypeNumberPad;
    
    //编辑时清空
    self.nickName.clearsOnBeginEditing = YES;
    self.mail.clearsOnBeginEditing = YES;
    self.RealName.clearsOnBeginEditing = YES;
    self.idNumber.clearsOnBeginEditing = YES;
    
    self.finish.layer.cornerRadius = 5;
    self.finish.layer.masksToBounds = YES;
    [self.finish.layer setBorderWidth:1.0]; //边框宽度
    [self.finish.layer setBorderColor:UIColorRGBA(19, 208, 202, 1).CGColor];//边框颜色
    [self.finish addTarget:self action:@selector(finishSender:) forControlEvents:UIControlEventTouchUpInside];
    
    //手势
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self addGestureRecognizer:tapGestureRecognizer];

    UIView *bigContainView = [self viewWithTag: 999];
    UITapGestureRecognizer *tapGestureRecognizer0 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [bigContainView addGestureRecognizer:tapGestureRecognizer0];
    
    //子view
    UIView *containView1 = [bigContainView viewWithTag: 1];
    
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView1 addGestureRecognizer:tapGestureRecognizer1];
    
    //子view
    UIView *containView2 = [bigContainView viewWithTag: 2];
    
    UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView2 addGestureRecognizer:tapGestureRecognizer2];
    
    //
    UIView *containView3 = [bigContainView viewWithTag: 3];
    
    UITapGestureRecognizer *tapGestureRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView3 addGestureRecognizer:tapGestureRecognizer3];
    
    UIView *containView4 = [bigContainView viewWithTag: 4];
    
    UITapGestureRecognizer *tapGestureRecognizer4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView4 addGestureRecognizer:tapGestureRecognizer4];
}

-(void)tap
{
    [self.nickName resignFirstResponder];
    [self.mail resignFirstResponder];
    [self.RealName resignFirstResponder];
    [self.idNumber resignFirstResponder];
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    
    switch (tag)
    {
        case 1:
            for (NSDictionary *dic in Response)
            {
                
            }
            break;
    }
}


- (void)finishSender:(id)sender
{
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    //    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:LoginInterface andParameter:@[@{@"phone":_phone.text}] tag:2];
}

- (IBAction)LoadUpHeadphoto:(id)sender {
//    if (!self.isComeJobD)
//    {
//        self.headPicker = [[UIImagePickerController alloc] init];
//        self.headPicker.mediaTypes = @[(NSString *)kUTTypeImage];
//        self.headPicker.allowsEditing = YES;
//        self.headPicker.delegate = self;
//        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//            WEAKSELF
//            [UIActionSheet showInView:self.view withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"拍照", @"从相册选择"] tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
//                switch (buttonIndex) {
//                    case 0:
//                        weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//                        break;
//                    case 1:
//                        weakSelf.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//                        break;
//                    default:
//                        break;
//                }
//                if (buttonIndex != actionSheet.cancelButtonIndex) {
//                    [weakSelf.navigationController presentViewController:weakSelf.headPicker animated:YES completion:nil];
//                }
//            }];
//        } else {
//            self.headPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            [self.navigationController presentViewController:self.headPicker animated:YES completion:nil];
//        }
//    }
}
-(void)uploadHeaderImg
{
//    [self.head setImage:self.img];
//    NSMutableDictionary * para=[[NSMutableDictionary alloc]init];
//    para[@"UID"]=self.uid;
//    para[@"TYPE"]=@"0";
//    para[@"RANDOMUSERID"]=[YYCAccount currentAccount].randomID;
//    NSLog(@"photo %@",para);
//    //1.获得请求管理者
//    [APIClient setUpToken:[YYCAccount currentAccount].autoToken];
//    //    AFHTTPRequestOperationManager *mgr = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:API_HOST]];
//    
//    //2 .发送POST请求
//    [[APIClient sharedClient] POST:UploadUserPhoto parameters:para constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        
//        UIImage *image = self.img;
//        NSData *data=[[NSData alloc]init];
//        data=UIImageJPEGRepresentation(image,1.0);
//        // 拼接文件参数uii
//        [formData appendPartWithFileData:data name:@"PHOTO" fileName:@"status.jpg" mimeType:@"image/jpg"];
//        [self.navigationController dismissViewControllerAnimated:YES completion:^{
//            
//        }];
//    } success:^(AFHTTPRequestOperation *operation, NSDictionary *statusDict)
//     {
//         YYCAccount * account=[YYCAccount currentAccount];
//         account.avatar=[statusDict objectForKey:@"PHOTOURL"];
//         //使用userDefaults 存储头像地址
//         [[NSUserDefaults standardUserDefaults] setObject:[statusDict objectForKey:@"PHOTOURL"] forKey:[NSString stringWithFormat:@"HeaderUrl_%@",self.uid]];
//         [[NSUserDefaults standardUserDefaults] synchronize];
//         [account save];
//         //             [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:[YYCAccount currentAccount].uid];
//         UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"头像上传成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//         [alertView show];
//         
//         NSString *url = [NSString stringWithFormat:@"%@%@user/photoDownload?UID=%@&FILEPATH=%@",API_HOST, base_IMG_URL,self.uid,[statusDict objectForKey:@"PHOTOURL"]];
//         NSString *name = [[url componentsSeparatedByString:@"/"] lastObject];
//         [self writeFile:name :self.img];
//         [[NSUserDefaults standardUserDefaults] setObject:name forKey:[NSString stringWithFormat:@"%@",url]];
//         [NSThread sleepForTimeInterval:2.0];
//     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
//     {
//         if (operation.response.statusCode == 998) {
//             [self.navigationController presentViewController:[[UINavigationController alloc] initWithRootViewController:[TCHomeViewController create]] animated:YES
//                                                   completion:^{
//                                                       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"用户验证失败，请重新登录" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
//                                                       [alert show];
//                                                       
//                                                   }];
//             return;
//         }
//         NSString *errorString = [[operation responseObject] objectForKey:@"RETURN_MSG"];
//         if (errorString.length != 0) {
//             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:errorString delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
//             [alert show];
//         }else
//         {
//             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您当前的网络不给力" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
//             [alert show];
//         }
//     }];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//    if (picker == self.headPicker)
//    {
//        [picker dismissViewControllerAnimated:YES completion:^{
//            UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
//            NSDictionary *profileAvatarImage = @{kNotificationShouldRefreshProfile : editedImage};
//            self.img= editedImage;
//            [self uploadHeaderImg];
//        }];
//    }else if(picker == self.mediaPicker){
//        [picker dismissViewControllerAnimated:YES completion:^{
//            
//        }];
//        [self.alumbView addPhoto:[info objectForKey:UIImagePickerControllerEditedImage]];
//    }
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)setHeaderImage
{
//    NSString *string =[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"HeaderUrl_%@",self.uid]];
//    NSString *url;
//    if (string!=nil&&![string isEqualToString:@""])
//    {
//        NSError *error=nil;
//        url=[NSString stringWithFormat:@"%@%@user/photoDownload?UID=%@&FILEPATH=%@",API_HOST,base_IMG_URL,self.uid,string];
//        
//        
//    }else
//    {
//        if (self.isComeJobD ) {
//            url=[NSString stringWithFormat:@"%@%@user/photoDownload?UID=%@&FILEPATH=%@",API_HOST,base_IMG_URL,self.uid,self.url];
//        }
//    }
//    if (url)
//    {
//        [self.HeadPhoto setImage:[UIImage imageNamed:@"header_img"]];
//        [self.HeadPhoto sd_setImageWithRequestURL:url];
//    }
//    
}

-(void)chosePhoto
{
//    self.isChangeHeaderPhoto = YES;
//    self.mediaPicker = [[UIImagePickerController alloc] init];
//    self.mediaPicker.mediaTypes = @[(NSString *)kUTTypeImage];
//    self.mediaPicker.allowsEditing = YES;
//    self.mediaPicker.delegate = self;
//    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//        WEAKSELF
//        [UIActionSheet showInView:self.view withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"拍照", @"从相册选择"] tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
//            switch (buttonIndex) {
//                case 0:
//                    weakSelf.mediaPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//                    break;
//                case 1:
//                    weakSelf.mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//                    break;
//                default:
//                    break;
//            }
//            if (buttonIndex != actionSheet.cancelButtonIndex) {
//                [weakSelf.navigationController presentViewController:weakSelf.mediaPicker animated:YES completion:nil];
//            }
//        }];
//    } else {
//        self.mediaPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//        [self.navigationController presentViewController:self.mediaPicker animated:YES completion:nil];
//    }
}
-(void)deleteLocalItemDicforKye:(NSString *)key
{
//    if ([self.imageDic.allKeys containsObject:key])
//    {
//        [self.imageDic removeObjectForKey:key];
//    }
}
-(void)setImageDic:(UIImage *)image forKey:(NSString *)key
{
//    self.isChangeHeaderPhoto = YES;
//    [self.imageDic setObject:image forKey:key];
}
-(void)deleteImageDicforKey:(NSString *)key
{
//    self.isChangeHeaderPhoto = YES;
//    [self.imageDic removeObjectForKey:key];
//    [self deleteAlum:key];
}


@end




















